# Credits:
# hyperheart - For coming up with concept and original coding.
from es import getplayerhandle, createentityindexlist, getindexprop, setindexprop, entitysetvalue, fire, precachemodel, doblock
from gamethread import delayed
from superdice.superdice import registerRoll, unregisterRoll, players

def es_map_start(ev):
    global model

def unload():
    unregisterRoll(diceRoll)
    doblock('corelib/noisy_off')

def player_spawn(ev):
    players[ev['userid']]['aimn'] = False

def weapon_fire(ev):
    userid = ev['userid']
    weapon = ev['weapon']
    if not players[userid]['aimn'] or not weapon in grenadeWeapons:
        return

    delayed(0.2, changeNade, (userid, getplayerhandle(userid), weapon, grenadeWeapons.index(weapon)))

def changeNade(userid, handle, nade, mode):
    for index in createentityindexlist(nade + '_projectile'):
        if not getindexprop(index, 'CBaseEntity.m_hOwnerEntity') == handle:
            continue

        entitysetvalue(index, 'targetname', index)
        entitysetvalue(index, 'gravity', 0.1)
        setindexprop(index, 'CBaseEntity.m_nModelIndex', precachemodel('models/roller_spikes.mdl'))
        if mode == 0:
            fire(userid, index, 'addoutput', 'rendercolor 255 0 0')

        elif mode == 1:
            fire(userid, index, 'addoutput', 'rendercolor 0 255 0')

        else:
            fire(userid, index, 'addoutput', 'rendercolor 0 0 255')

def diceRoll(userid, player, eplayer):
    players[userid]['aimn'] = True

doblock('corelib/noisy_on')

grenadeWeapons = ['hegrenade', 'smokegrenade', 'flashbang']

registerRoll(diceRoll, 'aimnade')
